<template>
  <div class="login-container">

    <el-form :model="loginForm"
             status-icon
             :rules="rules"
             ref="loginForm"
             label-width="100px"
             class="login-loginForm">
      <h1>RMB管理系统</h1>
      <div class="input">
        <el-form-item label="用户名"
                      prop="username">
          <el-input type="text"
                    v-model="loginForm.username"
                    autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码"
                      prop="password">
          <el-input type="password"
                    v-model="loginForm.password"
                    autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     @click="submitForm('loginForm')">提交</el-button>
        </el-form-item>
      </div>

    </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      var validateUsn = (rule, value, callback) => {
        //用户名正则，3到16位（字母，数字，下划线，减号）
        var uPattern = /^[a-zA-Z0-9_-]{3,16}$/;
        if (uPattern.test(value)) {
          callback()
        } else {
          callback("3到16位(字母，数字，下划线，减号)")
        }
      };

      var validatePsw = (rule, value, callback) => {
        if (!value) {
          callback("密码输入不能为空")
        } else {
          callback()
        }
      };
      return {
        loginForm: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { validator: validateUsn, trigger: 'blur' }
          ],
          password: [
            { validator: validatePsw, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {//本地校验通过
            alert('submit!');
          } else {//本地校验不通过
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped lang="less">
  .login-loginForm {
    border-radius: 10px;
    width: 400px;
    height: 485px;
    position: fixed;
    background: rgba(0, 0, 0, 0.3);
    right: 150px;
    bottom: 150px;
    box-sizing: border-box;
    & /deep/ .el-input__inner {
      //深度选择器
      width: 250px;
    }
    .input {
      margin-top: 180px;
    }
    & /deep/ .el-form-item {
      padding-right: 60px;
    }
  }
</style>